<?php
/**
 * Created by PhpStorm.
 * User: MF
 * Date: 2017/6/8
 * Time: 21:22
 */

namespace App\Repositories;
use App\Message;
/**
 * Class MessageRepository
 * @package App\Repositories
 */
class MessageRepository
{
	/**
	 * @param array $attributes
	 * @return static
	 */
	public function create(array $attributes)
	{
		return Message::create($attributes);
	}
	/**
	 * @return mixed
	 */
	public function getAllMessages()
	{
		return Message::where('to_user_id', user()->id)
			->orWhere('from_user_id', user()->id)
			->with(['fromUser' => function ($query) {
				return $query->select(['id', 'name', 'avatar']);
			}, 'toUser'        => function ($query) {
				return $query->select(['id', 'name', 'avatar']);
			}])->get();
	}
	/**
	 * @param $dialogId
	 * @return mixed
	 */
	public function getDialogMessagesBy($dialogId)
	{
		return Message::where('dialog_id',$dialogId)->with(['fromUser' => function ($query) {
			return $query->select(['id','name','avatar']);
		},'toUser' => function ($query) {
			return $query->select(['id','name','avatar']);
		}])->latest()->get();
	}
	/**
	 * @param $dialogId
	 * @return mixed
	 */
	public function getSingleMessageBy($dialogId)
	{
		return Message::where('dialog_id',$dialogId)->first();
	}
}